package com.heluo.eduservice.service.impl;

import com.alibaba.excel.EasyExcel;
import com.heluo.eduservice.entity.EduSubject;
import com.heluo.eduservice.entity.excel.SubjectData;
import com.heluo.eduservice.entity.subject.OneSubject;
import com.heluo.eduservice.entity.subject.TwoSubject;
import com.heluo.eduservice.listener.SubjectExcelListener;
import com.heluo.eduservice.mapper.EduSubjectMapper;
import com.heluo.eduservice.service.EduSubjectService;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 * 课程科目 服务实现类
 * </p>
 *
 * @author testjava
 * @since 2022-04-06
 */
@Service
public class EduSubjectServiceImpl extends ServiceImpl<EduSubjectMapper, EduSubject> implements EduSubjectService {



    @Override
    public void saveSubject(MultipartFile file,EduSubjectService subjectService) {

        try {
            InputStream in= file.getInputStream();
            EasyExcel.read(in, SubjectData.class,new SubjectExcelListener(subjectService)).sheet().doRead();
        } catch (IOException e) {
            e.printStackTrace();
        }

    }

    @Override
    public List<OneSubject> getAllOneTwoSubject() {

        QueryWrapper<EduSubject> wrapper=new QueryWrapper<>();
        wrapper.eq("parent_id",0);
        List<EduSubject> eduSubjects = baseMapper.selectList(wrapper);


        QueryWrapper<EduSubject> wrapper2=new QueryWrapper<>();
        wrapper.ne("parent_id",0);
        List<EduSubject> eduSubjects1 = baseMapper.selectList(wrapper2);

        List<OneSubject> finalSubjectList=new ArrayList<>();
        for (int i = 0; i < eduSubjects.size(); i++) {
            EduSubject eduSubject = eduSubjects.get(i);
            OneSubject oneSubject = new OneSubject();
            BeanUtils.copyProperties(eduSubject,oneSubject);
            finalSubjectList.add(oneSubject);



            List<TwoSubject> twoFinalSubjectList=new ArrayList<>();
            for (int j = 0; j <eduSubjects1.size() ; j++) {
                EduSubject eduSubject1 = eduSubjects1.get(j);
                if (eduSubject1.getParentId().equals(eduSubject.getId())){

                TwoSubject twoSubject = new TwoSubject();
                BeanUtils.copyProperties(eduSubject1,twoSubject);
                twoFinalSubjectList.add(twoSubject);
                }
            }
            oneSubject.setChildren(twoFinalSubjectList);
        }


        return finalSubjectList;
    }
}
